{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5, 6}\n"
     ]
    }
   ],
   "source": [
    "class Dice:                           ### fig:dice1 ###\n",
    "    def __init__(self):\n",
    "        self.numbers = {1,2,3,4,5,6}\n",
    "        \n",
    "dice = Dice()\n",
    "print(dice.numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1 in dice.numbers   ### fig:1_in_dice ####"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "7 not in dice.numbers    ### fig:1_not_in_dice ####"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "[1, 5, 3, 5, 6, 4, 3, 3, 6, 4]\n"
     ]
    }
   ],
   "source": [
    "import random   # モジュールのインポート                        ### fig:dice2 ###\n",
    "\n",
    "class Dice:\n",
    "    def __init__(self):  self.numbers = {1,2,3,4,5,6}\n",
    "    def roll(self):      return random.choice(list(self.numbers))         # 追加\n",
    "    def rolls(self,num): return random.choices(list(self.numbers), k=num) # 追加\n",
    "        \n",
    "dice = Dice()\n",
    "### roll, rollsを使ってみましょう ###\n",
    "print(dice.roll()) \n",
    "print(dice.rolls(10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'R', 'W'}\n"
     ]
    }
   ],
   "source": [
    "S_bag = {\"R\",\"R\",\"R\",\"W\",\"W\",\"W\",\"W\"}       ### fig:wrong_set ###\n",
    "print(S_bag)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'R_1', 'R_3', 'R_2', 'W_1', 'W_3', 'W_4', 'W_2'}\n"
     ]
    }
   ],
   "source": [
    "S_bag = {\"R_1\",\"R_2\",\"R_3\",\"W_1\",\"W_2\",\"W_3\",\"W_4\"}       ### fig:ball_set ###\n",
    "print(S_bag)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{<__main__.Ball object at 0x1089ee048>, <__main__.Ball object at 0x1089ee470>, <__main__.Ball object at 0x1089ee080>, <__main__.Ball object at 0x1089eeb00>, <__main__.Ball object at 0x1089ee978>, <__main__.Ball object at 0x1089ee5c0>, <__main__.Ball object at 0x1089ee9e8>}\n"
     ]
    }
   ],
   "source": [
    "class Bag:                                                  ### fig:bag_and_ball ###\n",
    "    def __init__(self):\n",
    "        self.contents = set()  # 空の集合をひとつ作っておく\n",
    "        \n",
    "    def add(self,ball): \n",
    "        self.contents.add(ball) # ボールを袋に入れる操作\n",
    "\n",
    "class Ball:\n",
    "    def __init__(self, color):\n",
    "        self.color = color     # ボールは色を示す変数（文字列の予定）だけ持つ\n",
    "\n",
    "bag = Bag()                    # 袋を一つ作る\n",
    "\n",
    "for i in range(3):             # 赤い玉を3つ加える\n",
    "    bag.add(Ball(\"red\"))\n",
    "for i in range(4):             # 白い玉を4つ加える\n",
    "    bag.add(Ball(\"white\"))\n",
    "\n",
    "print(bag.contents)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['red', 'red', 'red', 'white', 'white', 'white', 'white']\n"
     ]
    }
   ],
   "source": [
    "class Bag:                                                  ### fig:bag_and_ball2 ###\n",
    "    def __init__(self):              # @@@省略@@@\n",
    "        self.contents = set()        # @@@省略@@@\n",
    "        \n",
    "    def add(self,ball):              # @@@省略@@@\n",
    "        self.contents.add(ball)      # @@@省略@@@\n",
    "        \n",
    "    def pop(self):                              # このメソッドを追加\n",
    "        if not self.contents:\n",
    "            return None                        #　空ならNoneを返す\n",
    "        \n",
    "        b = random.choice(list(self.contents))\n",
    "        self.contents.remove(b)                 # setから選んだボールを消す\n",
    "        return b\n",
    "\n",
    "class Ball:                       # @@@省略@@@\n",
    "    def __init__(self, color):   # @@@省略@@@\n",
    "        self.color = color       # @@@省略@@@\n",
    "\n",
    "bag = Bag()\n",
    "for i in range(3):\n",
    "    bag.add(Ball(\"red\"))\n",
    "for i in range(4):\n",
    "    bag.add(Ball(\"white\"))\n",
    "\n",
    "result = []                 # 選んだボールの色を記録するためのリスト\n",
    "b = bag.pop()\n",
    "while b:\n",
    "    result.append(b.color)\n",
    "    b = bag.pop()\n",
    "        \n",
    "print(result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.5\n",
      "0.6666666666666666\n",
      "0.0\n",
      "1.0\n"
     ]
    }
   ],
   "source": [
    "class Agent:                      ### fig:dice_probability ###\n",
    "    def P_dice(self, dice, event):\n",
    "        return len(event)/len(dice.numbers)\n",
    "\n",
    "agent = Agent()\n",
    "print(agent.P_dice(dice, {2,4,6}))         # 偶数\n",
    "print(agent.P_dice(dice, {3,4,5,6}))       # 3以上\n",
    "print(agent.P_dice(dice, {}))              # 要素が空\n",
    "print(agent.P_dice(dice, {1,2,3,4,5,6}))   # 全要素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "object of type 'int' has no len()",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-11-dc7e169f5fca>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0magent\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mP_dice\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdice\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m        \u001b[0;31m# 集合にせずに出目を与える      ### fig:only_give_element ###\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m<ipython-input-10-28bc9d423fd9>\u001b[0m in \u001b[0;36mP_dice\u001b[0;34m(self, dice, event)\u001b[0m\n\u001b[1;32m      1\u001b[0m \u001b[0;32mclass\u001b[0m \u001b[0mAgent\u001b[0m\u001b[0;34m:\u001b[0m                      \u001b[0;31m### fig:dice_probability ###\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      2\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mP_dice\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdice\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mevent\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m         \u001b[0;32mreturn\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mevent\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdice\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnumbers\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m      4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      5\u001b[0m \u001b[0magent\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mAgent\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: object of type 'int' has no len()"
     ]
    }
   ],
   "source": [
    "print(agent.P_dice(dice, 3))        # 集合にせずに出目を与える      ### fig:only_give_element ###"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{EmptySet(), {1}, {2}, ..., {1, 3, 4, 5, 6}, {2, 3, 4, 5, 6}, {1, 2, 3, 4, 5, 6}}\n"
     ]
    }
   ],
   "source": [
    "import sympy                           ### fig:powerset ###\n",
    "s = sympy.FiniteSet(*dice.numbers)\n",
    "print(s.powerset())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{1, 2, 3, 4, 5, 6} x {1, 2, 3, 4, 5, 6} x {1, 2, 3, 4, 5, 6}\n",
      "{(4, 2, 2), (1, 4, 4), (2, 2, 4), (5, 5, 1), (5, 2, 1), (1, 4, 2), (5, 5, 3), (3, 1, 6), (5, 2, 3), (5, 5, 5), (3, 1, 4), (2, 6, 5), (3, 2, 2), (4, 1, 5), (3, 1, 2), (2, 6, 3), (6, 5, 5), (2, 5, 3), (4, 4, 2), (1, 2, 2), (6, 6, 3), (2, 6, 1), (6, 5, 3), (3, 2, 6), (2, 5, 1), (4, 6, 1), (4, 1, 1), (1, 2, 4), (6, 6, 1), (5, 3, 2), (1, 5, 5), (6, 5, 1), (3, 2, 4), (4, 6, 3), (4, 1, 3), (1, 2, 6), (2, 5, 5), (4, 6, 5), (1, 3, 5), (6, 3, 1), (4, 3, 6), (6, 6, 5), (5, 3, 6), (1, 5, 1), (3, 4, 5), (2, 3, 4), (1, 3, 3), (6, 3, 3), (4, 3, 4), (5, 6, 2), (5, 3, 4), (1, 5, 3), (2, 3, 6), (1, 3, 1), (6, 3, 5), (4, 3, 2), (5, 6, 4), (6, 4, 4), (3, 3, 1), (5, 1, 5), (3, 4, 1), (6, 2, 6), (5, 6, 6), (6, 4, 6), (3, 3, 3), (3, 4, 3), (2, 3, 2), (6, 2, 4), (6, 1, 2), (3, 3, 5), (2, 4, 2), (5, 1, 1), (3, 6, 6), (6, 2, 2), (5, 4, 3), (1, 6, 4), (6, 4, 2), (5, 1, 3), (2, 4, 4), (3, 6, 4), (4, 5, 2), (6, 1, 6), (5, 4, 1), (1, 6, 6), (2, 4, 6), (3, 6, 2), (2, 1, 5), (4, 5, 4), (4, 2, 5), (6, 1, 4), (3, 5, 2), (2, 2, 3), (4, 5, 6), (2, 1, 3), (5, 4, 5), (1, 6, 2), (2, 2, 1), (4, 4, 5), (2, 1, 1), (4, 2, 1), (1, 1, 2), (5, 2, 4), (3, 5, 6), (4, 4, 3), (4, 2, 3), (1, 1, 4), (5, 2, 6), (3, 5, 4), (2, 2, 5), (4, 4, 1), (1, 1, 6), (1, 4, 5), (5, 2, 2), (1, 4, 3), (5, 5, 2), (3, 1, 5), (2, 6, 6), (6, 5, 6), (1, 4, 1), (4, 1, 4), (5, 5, 4), (3, 1, 3), (2, 6, 4), (6, 5, 4), (3, 2, 3), (2, 5, 2), (4, 1, 6), (1, 2, 1), (5, 5, 6), (3, 1, 1), (2, 6, 2), (6, 5, 2), (3, 2, 1), (1, 2, 3), (6, 6, 2), (5, 3, 3), (2, 5, 6), (4, 1, 2), (1, 2, 5), (5, 3, 1), (1, 5, 4), (3, 2, 5), (2, 5, 4), (4, 6, 2), (1, 3, 6), (6, 6, 6), (1, 5, 6), (3, 4, 4), (4, 6, 4), (1, 3, 4), (6, 3, 2), (5, 6, 1), (6, 6, 4), (5, 3, 5), (3, 4, 6), (2, 3, 5), (4, 6, 6), (1, 3, 2), (6, 3, 4), (4, 3, 5), (5, 6, 3), (6, 4, 5), (1, 5, 2), (6, 3, 6), (4, 3, 3), (5, 6, 5), (5, 1, 4), (2, 4, 1), (3, 4, 2), (2, 3, 1), (6, 2, 5), (4, 3, 1), (6, 4, 1), (3, 3, 2), (2, 4, 3), (5, 1, 6), (2, 3, 3), (6, 2, 3), (6, 1, 3), (5, 4, 2), (6, 4, 3), (3, 3, 4), (2, 4, 5), (4, 5, 1), (2, 1, 6), (6, 2, 1), (6, 1, 1), (1, 6, 5), (3, 3, 6), (5, 1, 2), (3, 6, 5), (2, 1, 4), (4, 5, 3), (5, 4, 6), (3, 5, 3), (3, 6, 3), (2, 1, 2), (4, 5, 5), (4, 2, 4), (1, 1, 1), (6, 1, 5), (5, 4, 4), (1, 6, 1), (3, 5, 1), (2, 2, 2), (4, 4, 6), (3, 6, 1), (4, 2, 6), (1, 1, 3), (1, 6, 3), (4, 4, 4), (1, 1, 5), (5, 2, 5), (1, 4, 6), (3, 5, 5), (2, 2, 6)}\n",
      "216\n"
     ]
    }
   ],
   "source": [
    "s = sympy.FiniteSet(*dice.numbers)\n",
    "\n",
    "print(s**3)\n",
    "\n",
    "twice = set(e for e in s**3)\n",
    "print(twice)\n",
    "print(len(twice))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "216"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "6**3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
